﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace NRemedy.MVC.UI
{
    public class DataOptionBuilder
    {
        public IDictionary<string, object> Attributes {
            get;
            private set;
        }

        public IDictionary<string, string> Callbacks
        {
            get;
            private set;
        }

        public IDictionary<string, string> Object
        {
            get;
            private set;
        }


        public DataOptionBuilder()
        {
            Attributes = new SortedDictionary<string, object>(StringComparer.OrdinalIgnoreCase);
            Callbacks = new SortedDictionary<string, string>(StringComparer.OrdinalIgnoreCase);
            Object = new SortedDictionary<string, string>(StringComparer.OrdinalIgnoreCase);
        }

        public void MergeAttribute(string key,object value)
        {
            Attributes[key] = value;
        }

        public void MergeCallback(string key, string value)
        {
            Callbacks[key] = value;
        }

        public void MergeObject(string key, string value)
        {
            Object[key] = value;
        }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            foreach (var attribute in Attributes)
            {
                string key = attribute.Key;
                if (key == "styler" || key == "loader")
                {
                    sb.Append(key)
                    .Append(":")
                    .Append(attribute.Value)
                    .Append(",");
                }
                else if (attribute.Value is ValueType && !(attribute.Value is Boolean))
                {
                    sb.Append(key)
                    .Append(":")
                    .Append(attribute.Value)
                    .Append(",");
                }
                else if (attribute.Value is Boolean)
                {
                    sb.Append(key)
                    .Append(":")
                    .Append(attribute.Value.ToString().ToLower())
                    //.Append("'")
                    .Append(",");
                }
                else
                {
                    string value = HttpUtility.HtmlAttributeEncode(attribute.Value.ToString());
                    sb.Append(key)
                    .Append(":'")
                    .Append(value)
                    .Append("'")
                    .Append(",");
                }
                
            }

            foreach (var attribute in Callbacks)
            {
                string key = attribute.Key;
                //string value = HttpUtility.HtmlAttributeEncode(attribute.Value);
                string value = attribute.Value;
                sb.Append(key)
                  .Append(":")
                  .Append(value)
                  .Append(",");
            }

            foreach (var attribute in Object)
            {
                string key = attribute.Key;
                //string value = HttpUtility.HtmlAttributeEncode(attribute.Value);
                string value = attribute.Value;
                sb.Append(key)
                  .Append(":")
                  .Append(value)
                  .Append(",");
            }

            return sb.ToString().TrimEnd(',');
        }
    }
}
